program PREFIX;
const
  fi = 'prefix.in';
  fo = 'prefix.out';
  max = 200;
type
  Tnode = ^node;
  node = record
           stop : boolean;
           next : array['A'..'Z'] of Tnode;
         end;
var
  u : node;
  x : array[1..max] of char;
  d : array[0..max] of boolean;
  n, res : longint;

procedure init(u : node);
var
  c : char;
begin
  with u do
  begin
    for c := 'A' to 'Z' do next[c] := nil;
    stop := false;
  end;
end;

procedure update(s : string);
var
  i : longint;
  v : node;
begin
  if u.next[s[length(s)]] = nil then new(u.next[s[length(s)]]);
  u.next[s[length(s)]]^ := v;
  for i := length(s)-1 downto 1 do
  begin
    if v.next[s[i]] = nil then
    begin
      new(v.next[s[i]]);
      init(v.next[s[i]]^);
    end;
    v := v.next[s[i]]^;
  end;
  v.stop := true;
end;

procedure inp;
var
  s : string;
  p : longint;
begin
  init(u);
  while not eof do
  begin
    readln(s);
    if s = '.' then break;
    s := s + ' ';
    while length(s) > 0 do
    begin
      p := pos(' ', s);
      update(copy(s, 1, p-1));
      delete(s, 1, p);
    end;
  end;

  n := 0;
  while not seekeof do
  begin
    inc(n);
    read(x[n]);
  end;
end;

procedure solve(p : longint);
var
  t : longint;
  v : node;
begin
  t := p;
  v := u;
  repeat
    if v.stop and d[p-1] then
    begin
      d[t] := true;
      break;
    end;
    dec(p);
    if p = 0 then break;
    if v.next[x[p]] = nil then break;
    v := v.next[x[p]]^;
  until false;
end;

procedure process;
var
  i : longint;
begin
  res := -1;
  d[0] := true;
  for i := 1 to n do
  begin
    d[i] := false;
    solve(i);
    if d[i] then res := i;
  end;
end;

procedure out;
begin
  writeln(res);
end;

begin
  assign(input, fi); reset(input);
  assign(output, fo); rewrite(output);

  inp;
  process;
  out;

  close(output);
  close(input);
end.